//
//  UIImage+extension.swift
//  DLQRCode
//
//  Created by mac1012 on 2017/4/25.
//  Copyright © 2017年 DianlE. All rights reserved.
//

import UIKit

extension UIImage{
    
    func imageWithColor(color:UIColor) -> UIImage {
        return imageWithColor(color: color, frame: CGRect(x: 0, y: 0, width: 1, height: 1))
    }
    
    func imageWithColor(color:UIColor, frame:CGRect) -> UIImage {
        
        UIGraphicsBeginImageContext(frame.size)
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(color.cgColor)
        context?.fill(frame)
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img!
    }
    
    func imageFilter(name:String)->UIImage{
        if name == "No Filter" {
            return self
        }else{
        let imgOrientation = self.imageOrientation
        let imgScale = self.scale
        // Create and apply filter
        // 1 - create source image
        let sourceImage = CIImage(image: self)
        
        // 2 - create filter using name
        let myFilter = CIFilter(name: name)
        myFilter?.setDefaults()
        
        // 3 - set source image
        myFilter?.setValue(sourceImage, forKey: kCIInputImageKey)
        
        // 4 - create core image context
        let context = CIContext(options: nil)
        
        // 5 - output filtered image as cgImage with dimension.
        let outputCGImage = context.createCGImage(myFilter!.outputImage!, from: myFilter!.outputImage!.extent)
        
        // 6 - convert filtered CGImage to UIImage
        
        let filteredImage = UIImage(cgImage: outputCGImage!, scale: imgScale, orientation: imgOrientation)
        
        // 7 - set filtered image to array
        return filteredImage
    }
    }
    
    class func imageCompressFitSizeScale(sourceImage:UIImage,targetSize:CGSize)->UIImage{
        var newImgae:UIImage? = nil
        let imageH = sourceImage.size.height
        let imageW = sourceImage.size.width
        let targetH = targetSize.height
        let targetW = targetSize.width
        var scaleFactor:CGFloat = 0.0
        var thumbPoint = CGPoint(x: 0, y: 0)
        let scaleFactorH = targetH/imageH
        let scaleFactorW = targetW/imageW
        if scaleFactorH > scaleFactorW {
                scaleFactor = scaleFactorH
        }else{
                scaleFactor = scaleFactorW
        }
        
        let scaleW = scaleFactor * imageW
        let scaleH = scaleFactor * imageH
        
        if scaleFactorH > scaleFactorW {
            thumbPoint.x = (targetW - scaleW) * 0.5
        }else{
            thumbPoint.y = (targetH - scaleH) * 0.5
        }
        
        UIGraphicsBeginImageContext(targetSize);
        
        var thumbnailRect = CGRect(x: 0, y: 0, width: 0, height: 0);
        thumbnailRect.origin = thumbPoint;
        thumbnailRect.size.width = scaleW;
        thumbnailRect.size.height = scaleH;
        sourceImage.draw(in: thumbnailRect, blendMode: .normal, alpha: 1)
     
        newImgae = UIGraphicsGetImageFromCurrentImageContext()!;
        if(newImgae == nil){
            NSLog("scale image fail");
        }
        
        UIGraphicsEndImageContext();
        return newImgae!;

    
    }
  }
